package zozopo.attendance.operations;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import java.util.List;


import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.comm.OperatingService;

public class OperatorAttendanceOS extends OperatingService{
	
	
	public OperatorAttendanceOS(Connection conn){
		super(conn);
	}
	
	public OperatorAttendanceOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<OperatorAttendanceTB> selectListBySql(String sql){
		List<OperatorAttendanceTB> list = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		//Flow flow = new Flow(conn);
		//Map flowMap = flow.getFlowStateMapByKind(FlowCode.HOUSEWARE_RECEIPTLIST);
		if(resultSet != null){
				try {
					list = new ArrayList<OperatorAttendanceTB>();
					while(resultSet.next()){
						OperatorAttendanceTB obj = new OperatorAttendanceTB();
							
						
						obj.setId_operator(resultSet.getInt(OperatorAttendanceTB.ID_OPERATOR));
						obj.setOperatorid_operator(resultSet.getInt(OperatorAttendanceTB.OPERATORID_OPERATOR));
						
						obj.setName_operator(resultSet.getString(OperatorAttendanceTB.NAME_OPERATOR));
						obj.setFirstname_operator(resultSet.getString(OperatorAttendanceTB.FIRSTNAMENAME_OPERATOR));
						obj.setLastname_operator(resultSet.getString(OperatorAttendanceTB.LASTNAME_OPERATOR));
						
						obj.setMonday_operator(resultSet.getBoolean(OperatorAttendanceTB.MONDAY_OPERATOR));
						obj.setTuesday_operator(resultSet.getBoolean(OperatorAttendanceTB.TUESDAY_OPERATOR));
						obj.setWednesday_operator(resultSet.getBoolean(OperatorAttendanceTB.WEDNESDAY_OPERATOR));
						obj.setThursday_operator(resultSet.getBoolean(OperatorAttendanceTB.THURSDAY_OPERATOR));
						obj.setFriday_operator(resultSet.getBoolean(OperatorAttendanceTB.FRIDAY_OPERATOR));
						obj.setSaturday_operator(resultSet.getBoolean(OperatorAttendanceTB.SATURDAY_OPERATOR));
						obj.setSunday_operator(resultSet.getBoolean(OperatorAttendanceTB.SUNDAY_OPERATOR));
						
						
						obj.setBasicpay_operator(resultSet.getDouble(OperatorAttendanceTB.BASICPAY_OPERATOR));
						obj.setJobpay_operator(resultSet.getDouble(OperatorAttendanceTB.JOBPAY_OPERATOR));
						obj.setMarkpay_operator(resultSet.getDouble(OperatorAttendanceTB.MARKPAY_OPERATOR));
						
						obj.setWorkhours_operator(resultSet.getString(OperatorAttendanceTB.WORKHOURS_OPERATOR));
						obj.setOffhours_operator(resultSet.getString(OperatorAttendanceTB.OFFHOURS_OPERATOR));

						list.add(obj);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return list;
	
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<OperatorAttendanceTB> selectAll(){
		String sql = "SELECT * FROM " + OperatorAttendanceTB.TABLE_NAME;
		List<OperatorAttendanceTB> list =  selectListBySql(sql);
		
		return list;
	}
	
	
	
	
	public OperatorAttendanceTB get(int id){
		String sql = "SELECT * FROM " + OperatorAttendanceTB.TABLE_NAME + " where " + OperatorAttendanceTB.ID_OPERATOR + "=" + id;
		OperatorAttendanceTB obj = new OperatorAttendanceTB();
		List<OperatorAttendanceTB> list = selectListBySql(sql);
		obj = list.get(0); 
		return obj;
	}
	
	public OperatorAttendanceTB getInfoByOperatorid(int id){
		String sql = "SELECT * FROM " + OperatorAttendanceTB.TABLE_NAME + " where " + OperatorAttendanceTB.OPERATORID_OPERATOR + "=" + id;
		OperatorAttendanceTB obj = new OperatorAttendanceTB();
		List<OperatorAttendanceTB> list = selectListBySql(sql);
		obj = list.get(0); 
		return obj;
	}
	
	/**
	 * @version 1.0
	 * @see Gee
	 * */
	public boolean isExist(int id){
		String sql = "SELECT * FROM " + OperatorAttendanceTB.TABLE_NAME + " where " + OperatorAttendanceTB.OPERATORID_OPERATOR + "=" + id;
		List<OperatorAttendanceTB> list = selectListBySql(sql);
		if(list.size()>=1){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * @version 1.0
	 * @see Gee
	 * */
	public boolean isExistOperatorid(int id){
		String sql = "SELECT * FROM " + OperatorAttendanceTB.TABLE_NAME + " where " + OperatorAttendanceTB.OPERATORID_OPERATOR + "=" + id;
		List<OperatorAttendanceTB> list = selectListBySql(sql);
		if(list.size()>=1){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * @version 1.0
	 * @see Gee
	 * */
	public Map<Integer,List<OperatorAttendanceTB>> getWeekRestdayListMap(){
		String sql = "SELECT * FROM " + OperatorAttendanceTB.TABLE_NAME;
		
		Map<Integer,List<OperatorAttendanceTB>>  weekRestdayMap = new HashMap<Integer,List<OperatorAttendanceTB>>();
		List<OperatorAttendanceTB> list = selectListBySql(sql);
		
		ArrayList<OperatorAttendanceTB> sundayOperatorOfAttendanceList = new ArrayList<OperatorAttendanceTB>();
		ArrayList<OperatorAttendanceTB> mondayOperatorOfAttendanceList = new ArrayList<OperatorAttendanceTB>();
		ArrayList<OperatorAttendanceTB> tuesdayOperatorOfAttendanceList = new ArrayList<OperatorAttendanceTB>();
		ArrayList<OperatorAttendanceTB> wednesdayOperatorOfAttendanceList = new ArrayList<OperatorAttendanceTB>();
		ArrayList<OperatorAttendanceTB> thursdayOperatorOfAttendanceList = new ArrayList<OperatorAttendanceTB>();
		ArrayList<OperatorAttendanceTB> fridayOperatorOfAttendanceList = new ArrayList<OperatorAttendanceTB>();
		
		ArrayList<OperatorAttendanceTB> saturdayOperatorOfAttendanceList = new ArrayList<OperatorAttendanceTB>();
		
		for(OperatorAttendanceTB operatorOfAttendance:list){
			
			if(!operatorOfAttendance.isSunday_operator()){
				sundayOperatorOfAttendanceList.add(operatorOfAttendance);
			}
			
			if(!operatorOfAttendance.isMonday_operator()){
				mondayOperatorOfAttendanceList.add(operatorOfAttendance);
			}
			
			if(!operatorOfAttendance.isTuesday_operator()){
				tuesdayOperatorOfAttendanceList.add(operatorOfAttendance);
			}
			
			if(!operatorOfAttendance.isWednesday_operator()){
				wednesdayOperatorOfAttendanceList.add(operatorOfAttendance);
			}
			
			if(!operatorOfAttendance.isThursday_operator()){
				thursdayOperatorOfAttendanceList.add(operatorOfAttendance);
			}
			
			if(!operatorOfAttendance.isFriday_operator()){
				fridayOperatorOfAttendanceList.add(operatorOfAttendance);
			}
			
			if(!operatorOfAttendance.isSaturday_operator()){
				saturdayOperatorOfAttendanceList.add(operatorOfAttendance);
			}
			
		}
		weekRestdayMap.put(0, sundayOperatorOfAttendanceList);
		weekRestdayMap.put(1, mondayOperatorOfAttendanceList);
		weekRestdayMap.put(2, tuesdayOperatorOfAttendanceList);
		weekRestdayMap.put(3, wednesdayOperatorOfAttendanceList);
		weekRestdayMap.put(4, thursdayOperatorOfAttendanceList);
		weekRestdayMap.put(5, fridayOperatorOfAttendanceList);
		weekRestdayMap.put(6, saturdayOperatorOfAttendanceList);
		
		
		return weekRestdayMap;
	}
	

	
	public List<OperatorAttendanceTB> selectAll(int beginPageNum,int pageSize){
		
		List<OperatorAttendanceTB> list = null;
		String sql = "SELECT * FROM " + OperatorAttendanceTB.TABLE_NAME  
		+ " ORDER BY " + OperatorAttendanceTB.ID_OPERATOR + " DESC "
		+ " limit "+beginPageNum+","+pageSize; 
		list = selectListBySql(sql);
		
		return list;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNum(){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + OperatorAttendanceTB.TABLE_NAME ;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public List<OperatorAttendanceTB> selectBySearchStr(String searchStr,int beginPageNum,int pageSize){
		String sql = "select * from "+OperatorAttendanceTB.TABLE_NAME+" WHERE  "
		
		+OperatorAttendanceTB.NAME_OPERATOR+" LIKE '%"+searchStr+"%'"
		+" OR "+OperatorAttendanceTB.FIRSTNAMENAME_OPERATOR+" LIKE '%"+searchStr+"%'"
		+" OR "+OperatorAttendanceTB.LASTNAME_OPERATOR+" LIKE '%"+searchStr+"%'"
		+" limit "+beginPageNum+","+pageSize; 
		//ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		return selectListBySql(sql);
	}
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNumForSearchStr(String searchStr){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + OperatorAttendanceTB.TABLE_NAME +" WHERE  "

		+OperatorAttendanceTB.NAME_OPERATOR+" LIKE '%"+searchStr+"%'"
		+" OR "+OperatorAttendanceTB.FIRSTNAMENAME_OPERATOR+" LIKE '%"+searchStr+"%'"
		+" OR "+OperatorAttendanceTB.LASTNAME_OPERATOR+" LIKE '%"+searchStr+"%'";
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insert(OperatorAttendanceTB operator){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();

		insertMap.put(OperatorAttendanceTB.BASICPAY_OPERATOR, operator.getBasicpay_operator());
		
		insertMap.put(OperatorAttendanceTB.OPERATORID_OPERATOR, operator.getOperatorid_operator());
		insertMap.put(OperatorAttendanceTB.NAME_OPERATOR, operator.getName_operator());
		insertMap.put(OperatorAttendanceTB.LASTNAME_OPERATOR, operator.getLastname_operator());
		insertMap.put(OperatorAttendanceTB.FIRSTNAMENAME_OPERATOR, operator.getFirstname_operator());

		insertMap.put(OperatorAttendanceTB.MONDAY_OPERATOR, operator.isMonday_operator());
		insertMap.put(OperatorAttendanceTB.TUESDAY_OPERATOR, operator.isTuesday_operator());
		insertMap.put(OperatorAttendanceTB.WEDNESDAY_OPERATOR, operator.isWednesday_operator());
		insertMap.put(OperatorAttendanceTB.THURSDAY_OPERATOR, operator.isThursday_operator());
		insertMap.put(OperatorAttendanceTB.FRIDAY_OPERATOR, operator.isFriday_operator());
		insertMap.put(OperatorAttendanceTB.SATURDAY_OPERATOR, operator.isSaturday_operator());
		insertMap.put(OperatorAttendanceTB.SUNDAY_OPERATOR, operator.isSunday_operator());
		

		insertMap.put(OperatorAttendanceTB.WORKHOURS_OPERATOR, operator.getWorkhours_operator());
		insertMap.put(OperatorAttendanceTB.OFFHOURS_OPERATOR, operator.getOffhours_operator());
		
		return sqlMachine.insertRow(OperatorAttendanceTB.TABLE_NAME, insertMap);
	}
	
	/**
	 * @version 1.0
	 * @author gee
	 */
	public boolean update(OperatorAttendanceTB operator){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		
		updateMap.put(OperatorAttendanceTB.BASICPAY_OPERATOR, operator.getBasicpay_operator());
		
		updateMap.put(OperatorAttendanceTB.OPERATORID_OPERATOR, operator.getOperatorid_operator());
		updateMap.put(OperatorAttendanceTB.NAME_OPERATOR, operator.getName_operator());
		updateMap.put(OperatorAttendanceTB.LASTNAME_OPERATOR, operator.getLastname_operator());
		updateMap.put(OperatorAttendanceTB.FIRSTNAMENAME_OPERATOR, operator.getFirstname_operator());
		

		updateMap.put(OperatorAttendanceTB.MONDAY_OPERATOR, operator.isMonday_operator());
		updateMap.put(OperatorAttendanceTB.TUESDAY_OPERATOR, operator.isTuesday_operator());
		updateMap.put(OperatorAttendanceTB.WEDNESDAY_OPERATOR, operator.isWednesday_operator());
		updateMap.put(OperatorAttendanceTB.THURSDAY_OPERATOR, operator.isThursday_operator());
		updateMap.put(OperatorAttendanceTB.FRIDAY_OPERATOR, operator.isFriday_operator());
		updateMap.put(OperatorAttendanceTB.SATURDAY_OPERATOR, operator.isSaturday_operator());
		updateMap.put(OperatorAttendanceTB.SUNDAY_OPERATOR, operator.isSunday_operator());
	
		updateMap.put(OperatorAttendanceTB.WORKHOURS_OPERATOR, operator.getWorkhours_operator());
		updateMap.put(OperatorAttendanceTB.OFFHOURS_OPERATOR, operator.getOffhours_operator());
		
		return sqlMachine.updateRow(OperatorAttendanceTB.TABLE_NAME, updateMap, OperatorAttendanceTB.ID_OPERATOR, operator.getId_operator());
	}
	
	/**
	 * @version 1.0
	 * @author gee
	 */
	public boolean updateByOperatorId(OperatorAttendanceTB operator){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		
		updateMap.put(OperatorAttendanceTB.BASICPAY_OPERATOR, operator.getBasicpay_operator());
		updateMap.put(OperatorAttendanceTB.JOBPAY_OPERATOR, operator.getJobpay_operator());
		updateMap.put(OperatorAttendanceTB.MARKPAY_OPERATOR, operator.getMarkpay_operator());
		
		updateMap.put(OperatorAttendanceTB.OPERATORID_OPERATOR, operator.getOperatorid_operator());
		
		updateMap.put(OperatorAttendanceTB.NAME_OPERATOR, operator.getName_operator());
		updateMap.put(OperatorAttendanceTB.LASTNAME_OPERATOR, operator.getLastname_operator());
		updateMap.put(OperatorAttendanceTB.FIRSTNAMENAME_OPERATOR, operator.getFirstname_operator());
		

		updateMap.put(OperatorAttendanceTB.MONDAY_OPERATOR, operator.isMonday_operator());
		updateMap.put(OperatorAttendanceTB.TUESDAY_OPERATOR, operator.isTuesday_operator());
		updateMap.put(OperatorAttendanceTB.WEDNESDAY_OPERATOR, operator.isWednesday_operator());
		updateMap.put(OperatorAttendanceTB.THURSDAY_OPERATOR, operator.isThursday_operator());
		updateMap.put(OperatorAttendanceTB.FRIDAY_OPERATOR, operator.isFriday_operator());
		updateMap.put(OperatorAttendanceTB.SATURDAY_OPERATOR, operator.isSaturday_operator());
		updateMap.put(OperatorAttendanceTB.SUNDAY_OPERATOR, operator.isSunday_operator());
	
		updateMap.put(OperatorAttendanceTB.WORKHOURS_OPERATOR, operator.getWorkhours_operator());
		updateMap.put(OperatorAttendanceTB.OFFHOURS_OPERATOR, operator.getOffhours_operator());
		
		return sqlMachine.updateRow(OperatorAttendanceTB.TABLE_NAME, updateMap, OperatorAttendanceTB.OPERATORID_OPERATOR, operator.getOperatorid_operator());
	}

}
